{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "base_path = \"/mnt/newspace/dongxiaohang_space/AIStudy/OSMDataSets\"\n",
    "path_a = \"citylayout\"\n",
    "\n",
    "path_b = \"cityroad\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import glob\n",
    "from pathlib import Path\n",
    "\n",
    "layout = Path(base_path)/path_a\n",
    "\n",
    "layout = glob.glob(str(layout) + \"/*.png\")\n",
    "road = [lyt.replace(path_a, path_b) for lyt in layout ]\n",
    "\n",
    "pairs = list(zip(road, layout))\n",
    "\n",
    "for it in pairs:\n",
    "    if not Path(it[0]).exists() or not Path(it[1]).exists():\n",
    "        print(it)\n",
    "    if Path(it[0]).stem != Path(it[1]).stem:\n",
    "        print(it)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Path(\"results/nk_kids/ecapa_1986/train_speaker_embeddings.py\").stem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from speechbrain.utils.checkpoints import Checkpointer\n",
    "\n",
    "Checkpointer(\n",
    "    allow_partial_load=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nums = [ 2.46e-01, 1.54e-01, 8.10e-02 ]\n",
    "\n",
    "import math\n",
    "\n",
    "nums = [ round((1 - num)*100, 2) for num in nums ]\n",
    "\"\\n\".join(map(str, nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_3468863/15322234.py:4: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
      "  model = torch.load(model_path)\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'int' object has no attribute 'numel'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m/tmp/ipykernel_3468863/15322234.py\u001b[0m in \u001b[0;36m<cell line: 21>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     20\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     21\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mm\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mmodels\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 22\u001b[0;31m     \u001b[0mcompute_params\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mm\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/tmp/ipykernel_3468863/15322234.py\u001b[0m in \u001b[0;36mcompute_params\u001b[0;34m(model_path)\u001b[0m\n\u001b[1;32m      7\u001b[0m     \u001b[0;32mfor\u001b[0m \u001b[0mit\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      8\u001b[0m         \u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mparams\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mit\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m         \u001b[0mparams\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mparams\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnumel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     10\u001b[0m         \u001b[0;31m# print(f\"{name}: {params} parameters\")\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m         \u001b[0mtotal_params\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0mparams\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'int' object has no attribute 'numel'"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "def compute_params(model_path: str):\n",
    "    model = torch.load(model_path)\n",
    "    # print([ v[0] for v in model.items()])\n",
    "    total_params = 0\n",
    "    for it in model.items():\n",
    "        name, params = it\n",
    "        params = params.numel()\n",
    "        # print(f\"{name}: {params} parameters\")\n",
    "        total_params += params\n",
    "    print(f\"{round(total_params/1e6, 1)}\")\n",
    "\n",
    "\n",
    "import torch  \n",
    "\n",
    "# 假设你使用的是模型类，你需要实例化模型  \n",
    "model = YourModelClass()  # 替换为你的模型类  \n",
    "model.load_state_dict(torch.load('path_to_your_model.ckpt'))  \n",
    "\n",
    "# 计算参数数量  \n",
    "num_params = sum(p.numel() for p in model.parameters() if p.requires_grad)  \n",
    "print(f'Number of parameters in the model: {num_params}')\n",
    "models = [\n",
    "    \"/home/chenyang/chenyang_space/backup/Speech-Editing-Toolkit-linxi-dataset/checkpoints/editspeech_elderly/model_ckpt_steps_86000.ckpt\",\n",
    "    \"/home/chenyang/chenyang_space/backup/Speech-Editing-Toolkit-linxi-dataset/checkpoints/campnet_elderly/model_ckpt_steps_22000.ckpt\",\n",
    "    \"/home/chenyang/chenyang_space/backup/Speech-Editing-Toolkit-linxi-dataset/checkpoints/a3t_elderly/model_ckpt_steps_12000.ckpt\",\n",
    "    \"/home/chenyang/chenyang_space/backup/Speech-Editing-Toolkit-linxi-dataset/checkpoints/elderly_fluent/model_ckpt_steps_200000.ckpt\"\n",
    "   ]\n",
    "\n",
    "for m in models:\n",
    "    compute_params(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "modelscope",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
